<!-- 添加司机页面enterdriver.php -->
<!DOCTYPE html>
<html>

<?php
// 引入客户会话
include('session_client.php');
?>

<head>
    <link rel="shortcut icon" type="image/png" href="assets/img/P.png.png">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/fonts/font-awesome.min.css">
    <link rel="stylesheet" href="assets/w3css/w3.css">
    <link rel="stylesheet" type="text/css" href="assets/css/customerlogin.css">
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="assets/css/clientpage.css" />
</head>

<body>
    <!-- 导航栏 -->
    <?php
    include './nav.php';
    ?>

    <div class="container" style="margin-top: 65px;">
        <div class="col-md-7" style="float: none; margin: 0 auto;">
            <div class="form-area">
                <form role="form" id="addDriverForm" method="POST">
                    <br style="clear: both">
                    <h3 style="margin-bottom: 25px; text-align: center; font-size: 30px;"> 输入司机信息 </h3>

                    <div class="form-group">
                        <input type="text" class="form-control" id="driver_name" name="driver_name" placeholder="司机姓名" required autofocus="">
                    </div>

                    <div class="form-group">
                        <input type="text" class="form-control" id="dl_number" name="dl_number" placeholder="驾驶证号码" required>
                    </div>

                    <div class="form-group">
                        <input type="text" class="form-control" id="driver_phone" name="driver_phone" placeholder="联系方式" required>
                    </div>

                    <div class="form-group">
                        <input type="text" class="form-control" id="driver_address" name="driver_address" placeholder="地址" required>
                    </div>

                    <div class="form-group">
                        <label for="driver_gender">性别：</label>
                        <select class="form-control" id="driver_gender" name="driver_gender" required>
                            <option value="male">男性</option>
                            <option value="female">女性</option>
                        </select>
                    </div>

                    <button type="button" id="submit" class="btn btn-primary pull-right"> 添加司机</button>
                </form>
            </div>
        </div>
        <div class="col-md-9" style="float: none; margin: 0 auto;">
            <div class="form-area" style="padding: 0px 100px 100px 100px;">
                <form action="" method="POST">
                    <br style="clear: both">
                    <h3 style="margin-bottom: 25px; text-align: center; font-size: 30px;"> 我的司机 </h3>
                    <?php
                    // 存储会话
                    $user_check = $_SESSION['login_client'];
                    $sql = "SELECT * FROM driver d WHERE d.client_username='$user_check' ORDER BY driver_name";
                    $result = mysqli_query($conn, $sql);

                    if (mysqli_num_rows($result) > 0) {
                    ?>

                        <table class="table table-striped">
                            <thead class="thead-dark">
                                <tr>
                                    <th> </th>
                                    <th> 姓名</th>
                                    <th> 性别 </th>
                                    <th> 驾照号码 </th>
                                    <th> 联系方式 </th>
                                    <th> 地址 </th>
                                    <th> 可用性 </th>
                                    <th> 操作 </th>
                                </tr>
                            </thead>

                            <?php
                            // 输出每行的数据
                            while ($row = mysqli_fetch_assoc($result)) {
                            ?>

                                <tbody>
                                    <tr>
                                        <td> <span class="glyphicon glyphicon-menu-right"></span> </td>
                                        <td><?php echo $row["driver_name"]; ?></td>
                                        <td><?php echo $row["driver_gender"]; ?></td>
                                        <td><?php echo $row["dl_number"]; ?></td>
                                        <td><?php echo $row["driver_phone"]; ?></td>
                                        <td><?php echo $row["driver_address"]; ?></td>
                                        <td><?php echo $row["driver_availability"]; ?></td>
                                        <!-- 在表格中添加删除按钮 -->
                                        <td>
                                            <?php if ($row["driver_availability"] == 'yes') { ?>
                                                <a href="enterdriver_delete.php?driver_id=<?php echo $row['driver_id']; ?>" class="btn btn-danger" onclick="return confirmDelete()">删除</a>
                                            <?php } else { ?>
                                                <span class="text-muted">司机正在被使用中</span>
                                            <?php } ?>
                                        </td>


                                    </tr>
                                </tbody>

                            <?php } ?>
                        </table>
                        <br>

                    <?php } else { ?>

                        <h4>
                            <center>0 司机可用</center>
                        </h4>

                    <?php } ?>

                </form>

            </div>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            // 使用 jQuery 监听添加司机按钮点击事件
            $("#submit").click(function() {
                // 使用 jQuery 的 Ajax 方法发送异步请求
                $.ajax({
                    type: "POST",
                    url: "enterdriver1.php",
                    data: $("#addDriverForm").serialize(), // 序列化表单数据
                    success: function(response) {
                        // 请求成功时的操作
                        // 可以根据后端返回的响应执行不同的操作
                        // 在这里你可能需要更新司机列表等
                        alert("司机添加成功");
                        location.reload(); // 刷新页面或者更新司机列表
                    },
                    error: function() {
                        // 请求失败时的操作
                        alert("添加司机失败");
                    }
                });
            });

        });

        function confirmDelete() {
            return confirm("确定删除该司机吗？");
        }
    </script>

</body>
<footer class="site-footer">
    <div class="container">
        <hr>
        <div class="row">
            <div class="col-sm-6">
                <h5>© <?php echo date("Y"); ?> 租车服务</h5>
            </div>
        </div>
    </div>
</footer>

</html>